﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Image_Test1.ImageClasses;
using Image_Test1.ResizeClasses;
using System.Drawing.Imaging;

namespace Image_Test1
{
    public partial  class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            toolStrip1.Visible = false;
            toolStrip2.Visible = false;
            toolStrip3.Visible = false;
        }
        public Color[,] BufferImage;
        Bitmap BufferBitMap;
        public enum ResizeType
        {
            Biliner,BiCubic
        }
        public ResizeType resizeType = ResizeType.Biliner;

        public enum FilterType
        {
            Mean,Gaussian,Difference
        }
        public FilterType filterType = FilterType.Mean;

        public enum OtherType
        {
            Contrast, Brightness,Gamma,Binary,Add2Image
        }
        public OtherType otherType = OtherType.Contrast;
        
        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
            string File_Name = "";
            OpenFileDialog.Title = "Open Image";
            OpenFileDialog.FileName = "";
            //OpenFileDialog.Filter = "p3 Files *.ppm|*.ppm|P6 Files *.ppm|*.ppm|Bitmap Files *.bmp|*.bmp|JPEG Files *.jpg,*.jpeg|*.jpg|GIF Files *.gif|*.gif|PNG Files *.png|*.png|Icons *.ico|*.ico|All Files *.*|*.*";
            OpenFileDialog.Filter = "All Files *.*|*.*";

            if (OpenFileDialog.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("Cancel");
            }
            else
            {
                HandleImage Image = new HandleImage();
                File_Name = OpenFileDialog.FileName;
                Image.OpenImage(OpenFileDialog.FileName);
                BufferBitMap = Image.BitMap;
                BufferImage = new Color[BufferBitMap.Width, BufferBitMap.Height];

                for (int i = 0; i < BufferBitMap.Width; i++)
                {
                    for (int j = 0; j < BufferBitMap.Height; j++)
                    {
                        BufferImage[i, j] = BufferBitMap.GetPixel(i, j);   
                    }
                }

                Orignal_PicBox.Image = BufferBitMap;

                Statu_ToolStripStatusLabel.Text = string.Format("Width = {0} , Height = {1}",BufferBitMap.Width,BufferBitMap.Height);
            }

        }

        private void bilinearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resizeType = ResizeType.Biliner;
            HideToolStrip();
            toolStrip1.Visible = true;
        }

        private void biCubicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resizeType = ResizeType.BiCubic;
            HideToolStrip();
            toolStrip1.Visible = true;
        }

        private void Resize_ToolStripLabel_Click(object sender, EventArgs e)
        {

            int Width = int.Parse(Width_ToolStripTextBox.Text);
            int Height= int.Parse(Height_ToolStripTextBox.Text);
            if(resizeType== ResizeType.Biliner)
            {
                Bilinear Bi = new Bilinear();
                Bitmap Result = Bi.ResizeImage(new Size(Width, Height), BufferBitMap, BufferImage);

                Newimage_PicBox.Image = Result;
            }
            else
            {
                MessageBox.Show("Error");
            }
        }

        private void meanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            toolStrip2.Visible = true;
            filterType = FilterType.Mean;
            Mask_Sigma_Label.Text = "Mask : ";
            ToolStripButton.Text = "Mean Filter";
            Mask_Sigma_ToolStripTextBox.Visible = true;
            Choice_Sharpening_ToolStripComboBox.Visible = false;
        }

        private void gaussianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            toolStrip2.Visible = true;
            filterType = FilterType.Gaussian;
            Mask_Sigma_Label.Text = "Sigma : ";
            ToolStripButton.Text = "Gaussian Filter";
            Mask_Sigma_ToolStripTextBox.Visible = true;
            Choice_Sharpening_ToolStripComboBox.Visible = false;
        }

        private void differenceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            toolStrip2.Visible = true;
            filterType = FilterType.Difference;
            Mask_Sigma_Label.Text = "Choice : ";
            ToolStripButton.Text = "4 Difference Filter";
            Mask_Sigma_ToolStripTextBox.Visible = false;
            Choice_Sharpening_ToolStripComboBox.Visible = true;
        }

        private void ToolStripButton_Click(object sender, EventArgs e)
        {
            Bitmap Result = BufferBitMap;
            if (filterType == FilterType.Mean)
            {
                if (int.Parse(Mask_Sigma_ToolStripTextBox.Text) % 2 != 0)
                    Result = Mean_Filter();
                else
                    MessageBox.Show("Mask Value should be Even Value [ 1,3,5,7,....]");
            }
            else if (filterType == FilterType.Gaussian)
            {
                Result = Gaussian_Filter();
            }
            else if (filterType == FilterType.Difference)
            {
                if (Choice_Sharpening_ToolStripComboBox.SelectedIndex == 0)
                    Result = Sharpening_4Difference(0);
                else if (Choice_Sharpening_ToolStripComboBox.SelectedIndex == 1)
                    Result = Sharpening_4Difference(1);
                else if (Choice_Sharpening_ToolStripComboBox.SelectedIndex == 2)
                    Result = Sharpening_4Difference(2);
                else if (Choice_Sharpening_ToolStripComboBox.SelectedIndex == 3)
                    Result = Sharpening_4Difference(3);
                else
                    MessageBox.Show("Error Choice");
            }

            Newimage_PicBox.Image = Result;
        }

        private void laplacianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Bitmap Result = Laplacian_Sharpening();
            Newimage_PicBox.Image = Result;
        }

        private Bitmap Mean_Filter()
        {
            int OldWidth = BufferBitMap.Width, OldHigth = BufferBitMap.Height;
            Image_Test1.MyColor2 NewPixel = new Image_Test1.MyColor2();
            int Mask_Value = int.Parse(Mask_Sigma_ToolStripTextBox.Text);
            double Mean_Filter_Value = 1.0 / (Mask_Value * Mask_Value);
            int Padding = Mask_Value - 1;
            int NewWidth = OldWidth + Padding, NewHigth = OldHigth + Padding;
            int AddPixel = (Padding / 2);
            Bitmap NewBu = new Bitmap(NewWidth, NewHigth);
            Bitmap TempBu = new Bitmap(OldWidth, OldHigth);
            Color[,] NewBuffer = new Color[NewWidth, NewHigth];
            Color[,] TempBuffer = new Color[OldWidth, OldHigth];

            #region Make New Image With add Padding
            for (int i = 0; i < NewWidth; i++)
            {
                for (int j = 0; j < NewHigth; j++)
                {
                    if (i < AddPixel || j < AddPixel || i >= OldWidth + AddPixel || j >= OldHigth + AddPixel)
                        NewBuffer[i, j] = Color.FromArgb(0, 0, 0);
                    else
                        NewBuffer[i, j] = BufferImage[i - AddPixel, j - AddPixel];
                }
            }
            #endregion

            for (int i = 0; i < OldWidth; i++)
            {
                for (int j = 0; j < OldHigth; j++)
                {
                    for (int i1 = i; i1 < i + Mask_Value; i1++)
                    {
                        for (int j1 = j; j1 < j + Mask_Value; j1++)
                        {
                            NewPixel.Red += (NewBuffer[i1, j1].R * Mean_Filter_Value);
                            NewPixel.Green += (NewBuffer[i1, j1].G * Mean_Filter_Value);
                            NewPixel.Blue += (NewBuffer[i1, j1].B * Mean_Filter_Value);
                        }
                    }
                    TempBuffer[i, j] = Color.FromArgb((int)NewPixel.Red,(int) NewPixel.Green,(int) NewPixel.Blue);
                    TempBu.SetPixel(i, j, TempBuffer[i, j]);
                    NewPixel.Red = NewPixel.Green = NewPixel.Blue = 0;
                }
            }
            //DisplayPicture2.Image = TempBu;
            //ImageInfo2.Text = OldWidth.ToString() + " Width - " + OldHigth.ToString() + " Higth";

            return TempBu;

        }
        private Bitmap Gaussian_Filter()
        {
            //MessageBox.Show("Don't do anything .... !");
            int sigma = int.Parse(Mask_Sigma_ToolStripTextBox.Text);
            int N = (int)(3.7 * sigma - 0.5);
            int Mask_Size = 2 * N + 1;
            double[,] GaussMask = new double[Mask_Size, Mask_Size];

            int OldWidth = BufferBitMap.Width, OldHigth = BufferBitMap.Height;
            Image_Test1.MyColor2 NewPixel = new Image_Test1.MyColor2();

            int Padding = Mask_Size - 1;
            int AddPixel = (Padding / 2);
            int NewWidth = OldWidth + Padding, NewHigth = OldHigth + Padding;
            Bitmap NewBu = new Bitmap(NewWidth, NewHigth);
            //Bitmap TempBu = new Bitmap(OldWidth, OldHigth);
            Color[,] NewBuffer = new Color[NewWidth, NewHigth];
            Color[,] TempBuffer = new Color[OldWidth, OldHigth];

            //Make New Image With add Padding
            for (int i = 0; i < NewWidth; i++)
            {
                for (int j = 0; j < NewHigth; j++)
                {
                    if (i < AddPixel || j < AddPixel || i >= OldWidth + AddPixel || j >= OldHigth + AddPixel)
                        NewBuffer[i, j] = Color.FromArgb(0, 0, 0);
                    else
                        NewBuffer[i, j] = BufferImage[i - AddPixel, j - AddPixel];

                    //NewBu.SetPixel(i, j, NewBuffer[i, j]);
                }
            }

            //Fill GaussMask with Equation
            for (int i = -AddPixel; i <= AddPixel; i++)
            {
                for (int j = -AddPixel; j <= AddPixel; j++)
                {
                    GaussMask[i + AddPixel, j + AddPixel] = (1.0 / (2 * Math.PI * Math.Pow(sigma, 2))) * Math.Exp(-(Math.Pow(i, 2) + Math.Pow(j, 2)) / (2 * Math.Pow(sigma, 2)));
                }
            }

            for (int i = 0; i < OldWidth; i++)
            {
                for (int j = 0; j < OldHigth; j++)
                {
                    for (int i1 = i, i2 = 0; i1 < i + Mask_Size; i1++, i2++)
                    {
                        for (int j1 = j, j2 = 0; j1 < j + Mask_Size; j1++, j2++)
                        {
                            NewPixel.Red += (NewBuffer[i1, j1].R * GaussMask[i2, j2]);
                            NewPixel.Green += (NewBuffer[i1, j1].G * GaussMask[i2, j2]);
                            NewPixel.Blue += (NewBuffer[i1, j1].B * GaussMask[i2, j2]);
                        }
                    }
                    TempBuffer[i, j] = Color.FromArgb((int)NewPixel.Red, (int)NewPixel.Green, (int)NewPixel.Blue);
                    NewBu.SetPixel(i, j, TempBuffer[i, j]);
                    NewPixel.Red = NewPixel.Green = NewPixel.Blue = 0;
                }
            }
            //DisplayPicture2.Image = NewBu;
            //ImageInfo2.Text = OldWidth.ToString() + " Width - " + OldHigth.ToString() + " Higth";

            return NewBu;
        }
        private Bitmap Laplacian_Sharpening()
        {
            int OldWidth = BufferBitMap.Width, OldHigth = BufferBitMap.Height;
            const int Mask_Size = 3;
            int Padding = Mask_Size - 1;
            int AddPixel = (Padding / 2);
            int NewWidth = OldWidth + Padding, NewHigth = OldHigth + Padding;
            Color[,] NewBuffer = new Color[NewWidth, NewHigth];
            Image_Test1.MyColor2 NewPixel = new Image_Test1.MyColor2();

            Bitmap NewBu = new Bitmap(OldWidth, OldHigth);
            Color[,] TempBuffer = new Color[OldWidth, OldHigth];

            //Make New Image With add Padding
            for (int i = 0; i < NewWidth; i++)
            {
                for (int j = 0; j < NewHigth; j++)
                {
                    if (i < AddPixel || j < AddPixel || i >= OldWidth + AddPixel || j >= OldHigth + AddPixel)
                        NewBuffer[i, j] = Color.FromArgb(0, 0, 0);
                    else
                        NewBuffer[i, j] = BufferImage[i - AddPixel, j - AddPixel];

                    //NewBu.SetPixel(i, j, NewBuffer[i, j]);
                }
            }

            //Fill Mask of Laplacian Sharpening with Const Values
            double[,] LaplacianMask = new double[3, 3];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    LaplacianMask[i, j] = -1;
                }
            }
            LaplacianMask[1, 1] = 9;

            for (int i = 0; i < OldWidth; i++)
            {
                for (int j = 0; j < OldHigth; j++)
                {
                    for (int i1 = i, i2 = 0; i1 < i + Mask_Size; i1++, i2++)
                    {
                        for (int j1 = j, j2 = 0; j1 < j + Mask_Size; j1++, j2++)
                        {
                            NewPixel.Red += (NewBuffer[i1, j1].R * LaplacianMask[i2, j2]);
                            NewPixel.Green += (NewBuffer[i1, j1].G * LaplacianMask[i2, j2]);
                            NewPixel.Blue += (NewBuffer[i1, j1].B * LaplacianMask[i2, j2]);
                        }
                    }
                    #region Cutoff for Values is >255 & <0
                    if (NewPixel.Red > 255)
                        NewPixel.Red = 255;
                    if (NewPixel.Red < 0)
                        NewPixel.Red = 0;
                    if (NewPixel.Green > 255)
                        NewPixel.Green = 255;
                    if (NewPixel.Green < 0)
                        NewPixel.Green = 0;
                    if (NewPixel.Blue > 255)
                        NewPixel.Blue = 255;
                    if (NewPixel.Blue < 0)
                        NewPixel.Blue = 0;
                    #endregion

                    TempBuffer[i, j] = Color.FromArgb((int)NewPixel.Red, (int)NewPixel.Green, (int)NewPixel.Blue);
                    NewBu.SetPixel(i, j, TempBuffer[i, j]);
                    NewPixel.Red = NewPixel.Green = NewPixel.Blue = 0;
                }
            }
            //DisplayPicture2.Image = NewBu;
            //ImageInfo2.Text = OldWidth.ToString() + " Width - " + OldHigth.ToString() + " Higth";

            return NewBu;


        }
        private Bitmap Sharpening_4Difference(int Choice)
        {
            int OldWidth = BufferBitMap.Width, OldHigth = BufferBitMap.Height;
            const int Mask_Size = 3;
            int Padding = Mask_Size - 1;
            int AddPixel = (Padding / 2);
            int NewWidth = OldWidth + Padding, NewHigth = OldHigth + Padding;
            Color[,] NewBuffer = new Color[NewWidth, NewHigth];
            MyColor2 NewPixel = new MyColor2();

            Bitmap NewBu = new Bitmap(OldWidth, OldHigth);
            Color[,] TempBuffer = new Color[OldWidth, OldHigth];

            //Make New Image With add Padding
            for (int i = 0; i < NewWidth; i++)
            {
                for (int j = 0; j < NewHigth; j++)
                {
                    if (i < AddPixel || j < AddPixel || i >= OldWidth + AddPixel || j >= OldHigth + AddPixel)
                        NewBuffer[i, j] = Color.FromArgb(0, 0, 0);
                    else
                        NewBuffer[i, j] = BufferImage[i - AddPixel, j - AddPixel];

                    //NewBu.SetPixel(i, j, NewBuffer[i, j]);
                }
            }

            //Fill Mask of Laplacian Sharpening with Const Values
            double[,] LaplacianMask = new double[3, 3];
            switch (Choice)
            {
                case 0:
                    LaplacianMask[1, 0] = 1;
                    LaplacianMask[1, 1] = 1;
                    LaplacianMask[1, 2] = -1;
                    break;
                case 1:
                    LaplacianMask[0, 1] = 1;
                    LaplacianMask[1, 1] = 1;
                    LaplacianMask[2, 1] = -1;
                    break;
                case 2:
                    LaplacianMask[0, 2] = -1;
                    LaplacianMask[1, 1] = 1;
                    LaplacianMask[2, 0] = 1;
                    break;
                case 3:
                    LaplacianMask[0, 0] = 1;
                    LaplacianMask[1, 1] = 1;
                    LaplacianMask[2, 2] = -1;
                    break;
            }

            //LaplacianMask[1, 1] = 9;

            for (int i = 0; i < OldWidth; i++)
            {
                for (int j = 0; j < OldHigth; j++)
                {
                    for (int i1 = i, i2 = 0; i1 < i + Mask_Size; i1++, i2++)
                    {
                        for (int j1 = j, j2 = 0; j1 < j + Mask_Size; j1++, j2++)
                        {
                            NewPixel.Red += (NewBuffer[i1, j1].R * LaplacianMask[i2, j2]);
                            NewPixel.Green += (NewBuffer[i1, j1].G * LaplacianMask[i2, j2]);
                            NewPixel.Blue += (NewBuffer[i1, j1].B * LaplacianMask[i2, j2]);
                        }
                    }

                    #region Cutoff for Values is >255 & <0
                    if (NewPixel.Red > 255)
                        NewPixel.Red = 255;
                    if (NewPixel.Red < 0)
                        NewPixel.Red = 0;
                    if (NewPixel.Green > 255)
                        NewPixel.Green = 255;
                    if (NewPixel.Green < 0)
                        NewPixel.Green = 0;
                    if (NewPixel.Blue > 255)
                        NewPixel.Blue = 255;
                    if (NewPixel.Blue < 0)
                        NewPixel.Blue = 0;
                    #endregion

                    TempBuffer[i, j] = Color.FromArgb((int)NewPixel.Red, (int)NewPixel.Green, (int)NewPixel.Blue);
                    NewBu.SetPixel(i, j, TempBuffer[i, j]);
                    NewPixel.Red = NewPixel.Green = NewPixel.Blue = 0;
                }
            }
            //DisplayPicture2.Image = NewBu;
            //ImageInfo2.Text = OldWidth.ToString() + " Width - " + OldHigth.ToString() + " Higth";

            return NewBu;
        }

        private void withMatLabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Form1 form1 = new Form1();
            form1.Show();
            form1.Intiallize(BufferBitMap);
        }

        private void filtersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Form1 form1 = new Form1();
            form1.Show();
            form1.Intiallize(BufferBitMap);
        }

        private void magnitudeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Form2 form2 = new Form2();
            form2.Show();
            form2.Intiallize(BufferBitMap);
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toGrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Newimage_PicBox.Image = GrayImage();
        }
       
        private void NotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            Newimage_PicBox.Image = InvertImage();
        }

        private Bitmap GrayImage()
        {

            int Width = BufferBitMap.Width;
            int Height = BufferBitMap.Height;
            int R, G, B;
            Bitmap Result = new Bitmap(BufferBitMap.Width, BufferBitMap.Height);
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    R = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    G = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    B = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    Result.SetPixel(i, j, Color.FromArgb(R, G, B));
                }
            }
            return Result;
        }
        private Bitmap InvertImage()
        {
            int Width = BufferBitMap.Width;
            int Height = BufferBitMap.Height;
            int R, G, B;
            Bitmap Result = new Bitmap(BufferBitMap.Width, BufferBitMap.Height);
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    R = 255 - BufferBitMap.GetPixel(i, j).R;
                    G = 255 - BufferBitMap.GetPixel(i, j).G;
                    B = 255 - BufferBitMap.GetPixel(i, j).B;
                    Result.SetPixel(i, j, Color.FromArgb(R, G, B));
                }
            }
            return Result;
        }
        private Bitmap BinaryImage(int Range)
        {
            int Width = BufferBitMap.Width;
            int Height = BufferBitMap.Height;
            int R, G, B;
            Bitmap Result = new Bitmap(BufferBitMap.Width, BufferBitMap.Height);
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    R = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    R = ((R > Range) ? 255 : 0);
                    G = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    G = ((G > Range) ? 255 : 0);
                    B = ((BufferBitMap.GetPixel(i, j).R + BufferBitMap.GetPixel(i, j).G + BufferBitMap.GetPixel(i, j).B) / 3);
                    B = ((B > Range) ? 255 : 0);
                    Result.SetPixel(i, j, Color.FromArgb(R, G, B));
                }
            }
            return Result;
        }

        private void add2ImagesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otherType = OtherType.Add2Image;
            Name_ToolStripLabel.Text = "Ratio (0 To 1) : ";
            HideToolStrip();
            Max_ToolStripTextBox.Visible = false;
            toolStrip3.Visible = true;
        }

        private void sub2ImagesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideToolStrip();
            #region Open new Image
            string File_Name = "";
            OpenFileDialog.Title = "Open Image";
            OpenFileDialog.FileName = "";
            //OpenFileDialog.Filter = "p3 Files *.ppm|*.ppm|P6 Files *.ppm|*.ppm|Bitmap Files *.bmp|*.bmp|JPEG Files *.jpg,*.jpeg|*.jpg|GIF Files *.gif|*.gif|PNG Files *.png|*.png|Icons *.ico|*.ico|All Files *.*|*.*";
            OpenFileDialog.Filter = "All Files *.*|*.*";

            Bitmap BufferBitMap2 = BufferBitMap;
            //Color[,] BufferImage2;
            if (OpenFileDialog.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("Cancel");
            }
            else
            {
                HandleImage Image = new HandleImage();
                File_Name = OpenFileDialog.FileName;
                Image.OpenImage(OpenFileDialog.FileName);
                BufferBitMap2 = Image.BitMap;
                //BufferImage2 = new Color[BufferBitMap2.Width, BufferBitMap2.Height];

                //for (int i = 0; i < BufferBitMap2.Width; i++)
                //{
                //    for (int j = 0; j < BufferBitMap2.Height; j++)
                //    {
                //        BufferImage2[i, j] = BufferBitMap2.GetPixel(i, j);
                //    }
                //}

                Newimage_PicBox.Image = BufferBitMap2;
            }
            #endregion

            ImgAlgebra alg = new ImgAlgebra();
            Bitmap AddReslut = alg.SubImages(BufferBitMap, BufferBitMap2);
            ResultsForm ShowingImage = new ResultsForm();
            ShowingImage.Init(AddReslut);
            ShowingImage.Show();
        }

        private void contrastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otherType = OtherType.Contrast;
            Name_ToolStripLabel.Text = "( Min , Max ) ";
            HideToolStrip();
            toolStrip3.Visible = true;
        }

        private void brightnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otherType = OtherType.Brightness;
            Name_ToolStripLabel.Text = "Offset : ";
            HideToolStrip();
            Max_ToolStripTextBox.Visible = false;
            toolStrip3.Visible = true;
        }

        private void Go_ToolStripLabel_Click(object sender, EventArgs e)
        {
            #region If Contrast
            if (otherType == OtherType.Contrast)
            {
                Adjust adj = new Adjust();
                int Min = int.Parse(Min_ToolStripTextBox.Text);
                int Max = int.Parse(Max_ToolStripTextBox.Text);
                //MyColor[,] TempColor = new MyColor[BufferBitMap.Width, BufferBitMap.Height];
                //for (int i = 0; i < BufferBitMap.Width; i++)
                //{
                //    for (int j = 0; j < BufferBitMap.Height; j++)
                //    {
                //        TempColor[i, j].Red = BufferBitMap.GetPixel(i, j).R;
                //        TempColor[i, j].Green = BufferBitMap.GetPixel(i, j).G;
                //        TempColor[i, j].Blue = BufferBitMap.GetPixel(i, j).B;
                //    }
                //}
                //Newimage_PicBox.Image = adj.Contrast(BufferBitMap, Min, Max);
                MyColor[,] reslut;
                Bitmap Reslut = new Bitmap(BufferBitMap.Width, BufferBitMap.Height);

                reslut = adj.Contrast(BufferBitMap, Min, Max);
                for (int i = 0; i < BufferBitMap.Width; i++)
                {
                    for (int j = 0; j < BufferBitMap.Height; j++)
                    {
                        Reslut.SetPixel(i, j, Color.FromArgb(reslut[i, j].Red, reslut[i, j].Green, reslut[i, j].Blue));
                    }
                }
                Newimage_PicBox.Image = Reslut;
            } 
            #endregion
            #region If Brightness
            else if (otherType == OtherType.Brightness)
            {
                int offset = int.Parse(Min_ToolStripTextBox.Text);
                Adjust adj = new Adjust();
                MyColor[,] TempColor = new MyColor[BufferBitMap.Width, BufferBitMap.Height];
                for (int i = 0; i < BufferBitMap.Width; i++)
                {
                    for (int j = 0; j < BufferBitMap.Height; j++)
                    {
                        TempColor[i, j].Red = BufferBitMap.GetPixel(i, j).R;
                        TempColor[i, j].Green = BufferBitMap.GetPixel(i, j).G;
                        TempColor[i, j].Blue = BufferBitMap.GetPixel(i, j).B;
                    }
                }
                Newimage_PicBox.Image = adj.brightness(TempColor, offset);
            } 
            #endregion
            #region If Gamma
            else if (otherType == OtherType.Gamma)
            {
                Newimage_PicBox.Image = Gamma(BufferBitMap);
            } 
            #endregion
            #region If Binary
            else if (otherType == OtherType.Binary)
            {
                int Threshold = int.Parse(Min_ToolStripTextBox.Text);
                Newimage_PicBox.Image = BinaryImage(Threshold);
            } 
            #endregion
            #region If Add 2 Images
            else if (otherType == OtherType.Add2Image)
            {
                double Ratio = double.Parse(Min_ToolStripTextBox.Text);

                #region Open new Image
                string File_Name = "";
                OpenFileDialog.Title = "Open Image";
                OpenFileDialog.FileName = "";
                //OpenFileDialog.Filter = "p3 Files *.ppm|*.ppm|P6 Files *.ppm|*.ppm|Bitmap Files *.bmp|*.bmp|JPEG Files *.jpg,*.jpeg|*.jpg|GIF Files *.gif|*.gif|PNG Files *.png|*.png|Icons *.ico|*.ico|All Files *.*|*.*";
                OpenFileDialog.Filter = "All Files *.*|*.*";

                Bitmap BufferBitMap2 = BufferBitMap;
                //Color[,] BufferImage2;
                if (OpenFileDialog.ShowDialog() != DialogResult.OK)
                {
                    MessageBox.Show("Cancel");
                }
                else
                {
                    HandleImage Image = new HandleImage();
                    File_Name = OpenFileDialog.FileName;
                    Image.OpenImage(OpenFileDialog.FileName);
                    BufferBitMap2 = Image.BitMap;
                    //BufferImage2 = new Color[BufferBitMap2.Width, BufferBitMap2.Height];

                    //for (int i = 0; i < BufferBitMap2.Width; i++)
                    //{
                    //    for (int j = 0; j < BufferBitMap2.Height; j++)
                    //    {
                    //        BufferImage2[i, j] = BufferBitMap2.GetPixel(i, j);
                    //    }
                    //}

                    Newimage_PicBox.Image = BufferBitMap2;
                }
                #endregion

                //Newimage_PicBox.Image = BufferBitMap2;

                ImgAlgebra alg = new ImgAlgebra();
                Bitmap AddReslut = alg.addImages(BufferBitMap, BufferBitMap2, Ratio);
                ResultsForm ShowingImage = new ResultsForm();
                ShowingImage.Init(AddReslut);
                ShowingImage.Show();
            } 
            #endregion
            else
            {
                MessageBox.Show("Error");
            }
        }

        private void gammaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otherType = OtherType.Gamma;
            Name_ToolStripLabel.Text = "Power : ";
            HideToolStrip();
            Max_ToolStripTextBox.Visible = false;
            toolStrip3.Visible = true;
        }
        private Bitmap Gamma(Bitmap Image)
        {
            double[,] Red_value = new double[Image.Width, Image.Height];
            double[,] Green_value = new double[Image.Width, Image.Height];
            double[,] Blue_value = new double[Image.Width, Image.Height];
            double power = double.Parse(Min_ToolStripTextBox.Text);
            Bitmap new_img = new Bitmap(Image.Width, Image.Height);
            for (int i = 0; i < Image.Width; i++)
            {
                for (int j = 0; j < Image.Height; j++)
                {
                    Red_value[i, j] = Math.Pow(BufferImage[i, j].R, power);
                    Green_value[i, j] = Math.Pow(BufferImage[i, j].G, power);
                    Blue_value[i, j] = Math.Pow(BufferImage[i, j].B, power);

                }
            }
            Red_value = Normalization(Red_value, Image.Width, Image.Height);
            Blue_value = Normalization(Blue_value, Image.Width, Image.Height);
            Green_value = Normalization(Green_value, Image.Width, Image.Height);
            for (int i = 0; i < Image.Width; i++)
            {
                for (int j = 0; j < Image.Height; j++)
                {
                    new_img.SetPixel(i, j, Color.FromArgb((int)Red_value[i, j], (int)Green_value[i, j], (int)Blue_value[i, j]));

                }
            }

            return new_img;
        }
        public static double[,] Normalization(double[,] norm_arr, int Width, int Height)
        {
            double max = 0;
            double min = 1000;

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    if (norm_arr[i, j] > max)
                        max = norm_arr[i, j];
                    if (norm_arr[i, j] < min)
                        min = norm_arr[i, j];
                }
            }
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    if (max - min != 0)
                        norm_arr[i, j] = ((norm_arr[i, j] - min) / (max - min)) * 255;
                    else
                        norm_arr[i, j] = 0;
                }
            }
            return norm_arr;


        }

        private void Newimage_PicBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SaveFileDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            SaveFileDialog.Title = "Save an Image File";
            

            if (SaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && SaveFileDialog.FileName != "")
            {
                try
                {
                    using (Bitmap bitmap = new Bitmap(Newimage_PicBox.Image))
                    {
                        bitmap.Save(SaveFileDialog.FileName, ImageFormat.Jpeg);
                    }
                    MessageBox.Show("Picture Saved Successfully.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An Error has occured: \n" + ex.Message);
                } 
            }
            else
                MessageBox.Show("Error");

        }

        private void toBinaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otherType = OtherType.Binary;
            Name_ToolStripLabel.Text = "Threshold (0 To 255) : ";
            HideToolStrip();
            Max_ToolStripTextBox.Visible = false;
            toolStrip3.Visible = true;
            //Newimage_PicBox.Image = BinaryImage();
        }
        private void HideToolStrip()
        {
            toolStrip1.Visible = false;
            toolStrip2.Visible = false;
            toolStrip3.Visible = false;
        }
        
    }
    public struct MyColor
    {
        public int Red, Green, Blue;
    }
    public struct MyColor2
    {
        public double Red, Green, Blue;
    }
}

